/*
 * Login.java
 *
 * Created on 8 mars 2011, 20:55:16
 */

package UserInterface;

import BusinessLayer.Facade;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Marion
 */
public class Login extends JFrame implements UserInterface ,ActionListener,MouseListener , KeyListener {

    /** Creates new form Login */
    public Login() {
        this.display();

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        login = new javax.swing.JTextField();
        login.addKeyListener(this);
        pass = new javax.swing.JPasswordField();
        pass.addKeyListener(this);
        connexion = new javax.swing.JButton();
        connexion.addKeyListener(this);
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setForeground(new java.awt.Color(0, 153, 153));
        jLabel1.setText("Bienvenue sur l'application de gestion de notations");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("petitLogo.jpg"))); // NOI18N
        jLabel2.setText("jLabel2");

        login.setText("Nom d'utilisateur");

        pass.setText("");

       connexion.setText("Connexion");

        jLabel3.setForeground(new java.awt.Color(0, 153, 153));
        jLabel3.setText("- Cyr�ne- Polytech'Montpellier - 2011 - tous droits res�rv�s");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(293, 293, 293)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(165, 165, 165)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pass, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(login, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
                        .addGap(54, 54, 54)
                        .addComponent(connexion, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(254, 254, 254)
                        .addComponent(jLabel3)))
                .addContainerGap(171, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(connexion, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(131, 131, 131)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JButton connexion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField pass;
    private javax.swing.JTextField login;
    JOptionPane popup = new JOptionPane();
    

    /**
     * methode display
     */
    public void display() {
    	initComponents();
      	Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      	this.setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2);
        this.login.requestFocus();
        

        login.addMouseListener(this);
        

        //ajout d'un ecouteur de connexion

        connexion.addActionListener(this);
        connexion.addKeyListener(this);
        

        this.setMinimumSize(new Dimension(400, 350));
        this.setResizable(false);
        this.setVisible(true);
 
    }

    /**
     * action performed
     */
    public void actionPerformed(ActionEvent e) {
       //redirection lors de la connexion
       if (e.getSource()==connexion) {
            if(this.connexion()){

                 //on verifie que le mot de passe correspond au login
            	String login = this.login.getText();
            	@SuppressWarnings("deprecation")
				String mdp = this.pass.getText();
            	
            	if ( Facade.getInstance().checkID(login, mdp))
            	{
            		System.out.println("connexion....");
            		this.dispose();
            		Accueil suiv = new Accueil();
            	
            	}
            	else
            	{
            		System.out.println("erreur de connexion");
            		popup.showMessageDialog(null, "Veuillez entrez un login/mdp valide", "Probleme de connexion", JOptionPane.ERROR_MESSAGE);
            	}


            }
			      
       }


    }

    public boolean connexion()
    {
        return true;
    }


	/**
	 * efface le contenu du Jtextfield 
	 */
	public void mouseClicked(MouseEvent e) {
	 if (e.getSource()==login) {
	    	   login.setText(null);
	    	   
	    	   
	    	   
	       }
		// TODO Auto-generated method stub

	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}


	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}


	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}


	
	/**
	 * demarre la connexion quand la touche "entrer" est appuyer
	 */
	public void keyPressed(KeyEvent e) {
		
		 if (e.getKeyCode() == KeyEvent.VK_ENTER){
			 if(this.connexion()){

                 //on verifie que le mot de passe correspond au login
            	String login = this.login.getText();
            	@SuppressWarnings("deprecation")
				String mdp = this.pass.getText();
            	
            	if ( Facade.getInstance().checkID(login, mdp))
            	{
            		System.out.println("connexion....");
            		this.dispose();
            	Accueil suiv = new Accueil();
            	
            	}
            	else
            	{
            		System.out.println("erreur de connexion");
            		popup.showMessageDialog(null, "Veuillez entrez un login/mdp valide", "Probleme de connexion", JOptionPane.ERROR_MESSAGE);
            	}


            }
		 }
			
		 
		        

	
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	
	}



	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
